<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import {_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;

    let type_map = _config('status_alias_map.user_withdraw.type');
    let status_config = _config('status_alias_map.user_withdraw.status');
    let forms_config = {
      money: {label:'提现申请金额',name:'money',type:'text_one',config:'number',tips:'单位：元'},
      service_money: {label:'提现服务费',name:'service_money'},
      withdraw_money: {label:'到账金额',name:'withdraw_money',tips:'扣除服务费后的实际打款金额'},
      type: {label:'提现类型',name:'type',type:'select_one',config:type_map},
      withdraw_to: {label:'收款账号',name:'withdraw_to',type:'json_param',config:{},value:'[]'},
      need_bill: {label:'是否开票',name:'need_bill',type:'radio_switch',config:'否|0,是|1'},
      bill_file: {label:'发票文件',name:'bill_file',type:'file',download:1, is_info:true},
      note_user: {label:'用户备注',name:'note_user',type:'text_more',},
    }
    if (id){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      forms_config = Object.assign(forms_config, {
        note_admin: {label:'管理员备注',name:'note_admin',type:'text_more',tips:''},
        status: {label:'状态',name:'status',type:'radio',config:status_config,tips:'手动给收款账号打款后，选择【提现成功】后提交即可完成提现流程'},
      })
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }else {
      forms_config.service_money.remove = true;
      forms_config.withdraw_money.remove = true;
      forms_config = Object.assign({
        user_id: {label:'指定用户',name:'user_id',type:'list_id',config:'user/index',tips:''},
      }, forms_config)
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/user_withdraw/edit?id=`+this.id : '/admin/user_withdraw/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('user_withdraw/index', '$refs.list_url.list');
          iframe.hide('user_withdraw/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  watch: {
    'config.type.value'(v, oldV){
      let config = {};
      switch (v) {
        case 'wechat':{
          config = {
            truename: {label:'真实姓名',name:'truename',type:'text_one',tips:''},
            code: {label:'微信号',name:'code',type:'text_one',tips:''},
          }
        }break;
        case 'alipay':{
          config = {
            ali_name: {label:'用户名',name:'ali_name',type:'text_one',tips:''},
            code: {label:'账户',name:'code',type:'text_one',tips:''},
          }
        }break;
        case 'bank':{
          //{"code":"6214856895343323","truename":"玉峰","bank_name":"中国银行","bank_address":"重庆支行"}
          config = {
            truename: {label:'持卡人',name:'truename',type:'text_one',tips:''},
            code: {label:'卡号',name:'code',type:'text_one',tips:''},
            bank_name: {label:'银行',name:'bank_name',type:'text_one',tips:''},
            bank_address: {label:'开户银行',name:'bank_address',type:'text_one',tips:''},
          }
        }break;
      }
      this.config.withdraw_to.config = config;
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/user_withdraw/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('user_withdraw/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
  }
}
</script>
